#include "../mq_server/queue.h"
#include <gtest/gtest.h>
using namespace ns_queue;

shared_ptr<QueueManage> qmp;

class QueueTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        qmp = make_shared<QueueManage>("test/queue.db");
    }

    virtual void TearDown() override
    {
        // qmp->clear();
    }
};

TEST(QueueTest, insert_test)
{
    unordered_map<string, string> hash;
    hash["key1"] = "val1";
    hash["key2"] = "val2";
    qmp->declare_queue("q1", true, false, false, hash);
    qmp->declare_queue("q2", true, false, false, unordered_map<string, string>());
    qmp->declare_queue("q3", true, false, false, hash);
    ASSERT_EQ(qmp->size(), 3);
}

TEST(QueueTest, select_test)
{
    shared_ptr<Queue> qp = qmp->select_queue("q1");
    ASSERT_NE(qp.get(),nullptr);
    ASSERT_EQ(qp->_name, string("q1"));
    ASSERT_EQ(qp->_exclusive, false);
    ASSERT_EQ(qp->_IsDurable, true);
    ASSERT_EQ(qp->_auto_delete, false);
    ASSERT_EQ(qp->_args.size(),2);
    ASSERT_EQ(qp->_args["key1"], string("val1"));
    ASSERT_EQ(qp->get_args(), string("key2=val2&key1=val1"));
}

TEST(QueueTest, delete_test)
{
    qmp->exist("q1");
    qmp->exist("q2");
    qmp->delete_queue("q1");
    qmp->delete_queue("q2");
    ASSERT_EQ(qmp->size(), 1);
}

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new QueueTest);

    return RUN_ALL_TESTS();
}